package cn.com.surker.production.service.impl;

import cn.com.surker.enums.StateEnum;
import cn.com.surker.enums.production.indent.IsProEnum;
import cn.com.surker.enums.sale.BillTimeTypeEnum;
import cn.com.surker.production.dao.ProductionIndentDao;
import cn.com.surker.production.entity.ProductionIndent;
import cn.com.surker.production.entity.report.ProductionIndentReport;
import cn.com.surker.production.entity.vo.ProductionIndentVo;
import cn.com.surker.production.service.ProductionIndentService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 生产计划表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-07-25
 */
public abstract class ProductionIndentBaseImpl extends ServiceImpl<ProductionIndentDao, ProductionIndent> implements ProductionIndentService {

	@Resource
	protected ProductionIndentDao indentDao;

	@Override
	public Long findMaxErpId(String dbCode) {
		Long erpId = indentDao.findMaxErpId(dbCode);
		return erpId;
	}

	@Override
	public ProductionIndent findByErpId(String dbCode, Long erpId) {
		if (erpId == null) {
			return null;
		}
		ProductionIndent productionIndent = indentDao.findByErpId(dbCode, erpId);
		return productionIndent;
	}

	@Override
	public List<ProductionIndent> findList(String dbCode, String startTime, String endTime, String code, String conName, String levelName, String site) {
		QueryWrapper<ProductionIndent> qw = new QueryWrapper<>();
		qw.eq("status", StateEnum.ENABLED.getCode());
		if (StringUtils.isNotBlank(code)) {
			qw.like("code", code);
		}
		List<ProductionIndent> productionIndents = indentDao.selectListV2(dbCode, qw);
		return productionIndents;
	}

	@Override
	public ProductionIndent getBaseById(String dbCode, Long indentId) {
		ProductionIndent indent = indentDao.getBaseById(dbCode, indentId);
		return indent;
	}

	@Override
	public IPage<ProductionIndentVo> selectFullPage(String dbCode, String startTime, String endTime, String searchVal, Integer isPro, List<Integer> proTypes, Integer pageIndex, Integer limit) {
		QueryWrapper<ProductionIndent> qw = initFullParam(dbCode, startTime, endTime, searchVal, isPro, proTypes);
		PageHelper.startPage(pageIndex, limit);
		IPage<ProductionIndentVo> iPage = indentDao.selectFull(new Page<>((pageIndex + limit) / limit, limit), dbCode, qw);
		/**分页插件**/
		if (!StringUtils.isNull(iPage.getRecords())) {
			PageInfo pageInfo = new PageInfo(iPage.getRecords());
			if (!StringUtils.isNull(pageInfo.getTotal()))
				iPage.setTotal(pageInfo.getTotal());
		}
		return iPage;
	}

	@Override
	public List<ProductionIndentVo> selectFullList(String dbCode, String startTime, String endTime, String searchVal, Integer isPro, List<Integer> proTypes) {
		QueryWrapper<ProductionIndent> qw = initFullParam(dbCode, startTime, endTime, searchVal, isPro, proTypes);
		List<ProductionIndentVo> list = indentDao.selectFull(dbCode, qw);
		return list;
	}

	/**
	 * 初始化查询全部信息的参数,查询云端和本地库时候,仅仅是指向库的表不同,结构一样,所以进行统一
	 *
	 * @param startTime
	 * @param endTime
	 * @param searchVal
	 * @param isPro
	 * @param proTypes
	 * @return
	 */
	protected QueryWrapper<ProductionIndent> initFullParam(String dbCode, String startTime, String endTime, String searchVal, Integer isPro, List<Integer> proTypes) {
		QueryWrapper<ProductionIndent> qw = new QueryWrapper<>();
		if (StringUtils.isNotBlank(searchVal)) {
			//	根据模糊筛选
			qw.and(e -> {
				e.like("b.name", searchVal).or().like("a.site", searchVal).or().like("a.code", searchVal);
			});
		}
		if (isPro != null) {
			qw.eq("a.is_pro", isPro);
		}
		if (proTypes != null && proTypes.size() > 0) {
			qw.in("a.type", proTypes);
		}
		if (StringUtils.isNotBlank(startTime)) {
			qw.ge("a.create_time", startTime);
		}
		if (StringUtils.isNotBlank(endTime)) {
			qw.lt("a.create_time", endTime);
		}
		/*if (StringUtils.isNotBlank(startTime)) {
			qw.and(e -> {
				e.isNotNull("(SELECT MIN(inv.pro_time) FROM " + dbCode + ".production_invoice inv WHERE inv.indent_id = a.id)")
					.ge("(SELECT MIN(inv.pro_time) FROM " + dbCode + ".production_invoice inv WHERE inv.indent_id = a.id)", startTime);
			});
		}
		if (StringUtils.isNotBlank(endTime)) {
			qw.and(e -> {
				e.isNotNull("(SELECT MIN(inv.pro_time) FROM " + dbCode + ".production_invoice inv WHERE inv.indent_id = a.id)")
					.lt("(SELECT MIN(inv.pro_time) FROM " + dbCode + ".production_invoice inv WHERE inv.indent_id = a.id)", endTime);
			});
		}*/
		qw.eq("a.status", StateEnum.ENABLED.getCode());
		qw.groupBy("a.id");
		qw.orderByDesc("a.actual_start_time", "a.dispatch_start_time", "a.id");
		return qw;
	}

	@Override
	public void saveBatchV2(String dbCode, List<ProductionIndent> oaIndentList) {
		indentDao.saveBatchV2(dbCode, oaIndentList);
	}

	@Override
	public ProductionIndentVo queryFullById(String dbCode, Long id) {
		ProductionIndentVo indentVo = indentDao.queryFullById(dbCode, id);
		return indentVo;
	}
	/*@Override
	public UpdateChainWrapper<ProductionIndent> update() {
		return null;
	}*/

	@Override
	protected Class<ProductionIndent> currentModelClass() {
		return ProductionIndent.class;
	}
}